<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <a-button
        type="primary"
        v-auth="'dep:add'"
        preIcon="ant-design:plus-outlined"
        @click="handleCreate"
      >
        发布消息</a-button
      >
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--编辑抽屉-->
  <editDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  
  <RoleDesc @register="registerDesc"></RoleDesc>
</template>
<script lang="ts" name="doctor" setup>
import { ref } from "vue";
import { BasicTable, TableAction } from "/@/components/Table";
import { useDrawer } from "/@/components/Drawer";
import { useMessage } from "/@/hooks/web/useMessage";
import { columns, searchFormSchema } from "./index.data";
import { list, del,draw ,release} from "./index.api";
import { useListPage } from "/@/hooks/system/useListPage";
import { useSuccess } from "/@/hooks/rw/handleSuccess";
const { rhandleSuccess } = useSuccess();
import editDrawer from "./editDrawer.vue";
import { IResultTreeData } from "../../system/dict/dict.type";
const showFooter = ref(true);
const [roleUserDrawer, { openDrawer: openRoleUserDrawer }] = useDrawer();
const [registerDrawer, { openDrawer }] = useDrawer();
const [registerDesc, { openDrawer: openRoleDesc }] = useDrawer();
const { createMessage: msg, createConfirm } = useMessage();

// 列表页面公共参数、方法
const { prefixCls, tableContext, onImportXls, onExportXls } = useListPage({
  designScope: "role-template",
  tableProps: {
    title: "租户角色列表",
    api: list,
    columns: columns,
    size: "small",
    pagination: true,
    showTableSetting: true,
		showIndexColumn: true,
    tableSetting: { fullScreen: true },
    formConfig: {
      schemas: searchFormSchema,
      autoAdvancedCol: 4,
      baseColProps: { xs: 24, sm: 12, md: 6, lg: 6, xl: 6, xxl: 4 },
      actionColOptions: { xs: 24, sm: 12, md: 6, lg: 6, xl: 6, xxl: 4 },
    },
    actionColumn: {
      width: 70,
    },
    fetchSetting: {
      // 传给后台的当前页字段
      pageField: "pageIndex",
      // 传给后台的每页显示多少条的字段
      sizeField: "pageSize",
      // 接口返回表格数据的字段
      listField: "entities",
      // 接口返回表格总数的字段
    },
    actionColumn: {
      width: 70,
    },
  },
});
const [
  registerTable,
  { reload, setSelectedRowKeys },
  { rowSelection, selectedRowKeys },
] = tableContext;

/**
 * 新增
 */
function handleCreate() {
  console.log(openDrawer, "openDrawer");
  showFooter.value = true;
  openDrawer(true, {
    record: {},
    isUpdate: false,
  });
}
/**
 * 编辑
 */
function handleEdit(record: Recordable) {
  showFooter.value = true;
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}
/**
 * 重新发布
 */
 function republish(record) {
  console.log(record,'lllllllll')
  release(record.id,record).then((res:any) =>{
    openDrawer(true, {
    record,
    isUpdate: true,
  });
  })
}
/**
 * 撤销
 */
function handleDetail(record) {
  draw([record.id]).then((res:any) => {
    if (res.isSucceed) {
      msg.success(res.msg || "撤销成功");
    } else {
      msg.error(res.msg || "撤销失败");
    }
    reload();
  })
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  del([record.id]).then((res: IResultTreeData) => {
    if (res.isSucceed) {
      msg.success(res.msg || "删除成功");
    } else {
      msg.error(res.msg || "删除失败");
    }
    reload();
  });
}
/**
 * 批量删除事件
 */
// async function batchHandleDelete() {
//   createConfirm({
//     title: "注意",
//     content: `确定要删除这些数据吗？删除后不可还原`,
//     iconType: "warning",
//     onOk: async () => {
//       let id = selectedRowKeys.value;
//       const res = await del(id);
//       rhandleSuccess(res, reload);
//       setSelectedRowKeys([]);
//       selectedRowKeys.value = [];
//     },
//   });
// }
/**
 * 角色用户
 */
function handleUser(record) {
  //onSelectChange(selectedRowKeys)
  openRoleUserDrawer(true, record);
}
/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: "审核",
      onClick: handleEdit.bind(null, record),
      auth: "dep:edit",
      ifShow: () => {
                return record.status !== 'Published' && record.status !== "Withdraw";; // 根据业务控制是否显示: 非enable状态的不显示启用按钮
              },
    },
    {
      label: "撤销",
      popConfirm: {
        title: "是否确认撤销该发布",
        confirm: handleDetail.bind(null, record),
      },
      ifShow: () => {
                return record.status !== 'Published' && record.status !== "Withdraw";
              },
    },
    {
      label: "重新发布",
      onClick: republish.bind(null, record),
      ifShow: () => {
                return record.status == 'Withdraw';
              },
    },
    {
      label: "删除消息",
      auth: "dep:delete",
      popConfirm: {
        title: "是否确认撤销该发布",
        confirm: handleDelete.bind(null, record),
      },
      ifShow: () => {
                return record.status == 'Withdraw';
              },
    },
  ];
}
</script>
